package com.aauedu.auth.service.impl;

import com.aauedu.auth.mapper.SysRoleMapper;
import com.aauedu.auth.mapper.SysUserRoleMapper;
import com.aauedu.auth.service.SysRoleService;
import com.aauedu.model.system.SysRole;
import com.aauedu.model.system.SysUserRole;
import com.aauedu.vo.system.AssginRoleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    //1 查询所有角色 和 当前用户所属的角色
    @Override
    public Map<String, Object> findRoleDataByUserId(Long userId) {
        //1 查询所有角色,返回list集合
        List<SysRole> allRolesList = this.list();
        //2 根据userId查询角色用户关系表，查询userId对应所有roleId
        LambdaQueryWrapper<SysUserRole> sysUserRoleLambdaQueryWrapper = new LambdaQueryWrapper<SysUserRole>();
        sysUserRoleLambdaQueryWrapper.eq(SysUserRole::getUserId, userId).select(SysUserRole::getRoleId);
        List<SysUserRole> existUserRoleList = sysUserRoleMapper.selectList(sysUserRoleLambdaQueryWrapper);
        //从查询出来的用户id对应角色list集合，获取所有角色id
//        List<Long> existRoleIdList = new ArrayList<>();
//        for (SysUserRole sysUserRole : existUserRoleList) {
//            existRoleIdList.add(sysUserRole.getRoleId());
//        }
        //方式2：可以使用lambda8 stream流的方式来写
        List<Long> existRoleIdList = existUserRoleList.stream().map(c -> c.getRoleId()).collect(Collectors.toList());
        List<SysRole> assignRoleList = new ArrayList<>();
        //3 根据查询的所有角色id，找到对应角色信息
        //根据角色id的所有的角色的list集合进行比较
        for (SysRole sysRole : allRolesList) {
             //比较
            if (existRoleIdList.contains(sysRole.getId())) {
                assignRoleList.add(sysRole);
            }
        }
        //4 把得到的两部分数据封装map集合，进行返回
        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("assignRoleList", assignRoleList);
        roleMap.put("allRolesList", allRolesList);
        return roleMap;
    }

    //为用户分配角色
    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
        //1.删除用户原来的角色
        sysUserRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, assginRoleVo.getUserId()));
        //2.为用户重新分配角色
        Long userId = assginRoleVo.getUserId();
        List<Long> roleIdList = assginRoleVo.getRoleIdList();
        if (StringUtils.isEmpty(roleIdList)) {
            return;
        }
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(userId);
        for(Long roleId: roleIdList) {
            sysUserRole.setRoleId(roleId);
            sysUserRoleMapper.insert(sysUserRole);
        }
    }

    /*//2 为用户分配角色
    @Override
    @Transactional
    public void doAssign(AssginRoleVo assignRoleVo) {
        //把用户之前分配角色数据删除，用户角色关系表里面，根据userId删除
        sysUserRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, assignRoleVo.getUserId()));
        //重新进行分配
        if (StringUtils.isEmpty(assignRoleVo.getRoleIdList()))
            return;
        for (Long roleId : assignRoleVo.getRoleIdList()) {
            if (StringUtils.isEmpty(roleId)) continue;
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setRoleId(roleId);
            sysUserRole.setUserId(assignRoleVo.getUserId());
            sysUserRoleMapper.insert(sysUserRole);
        }
    }*/


    //为用户分配角色



}
